//
// Created by Semir on 2020/5/15.
//

#include <iostream>

using namespace std;

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    explicit TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

void ConnectTreeNode(TreeNode* pRoot, TreeNode* pLeft, TreeNode* pRight) {
    if (pRoot == nullptr) {
        return;
    }
    pRoot->left = pLeft;
    pRoot->right = pRight;
}

TreeNode* BuildTree1() {
    auto* node1 = new TreeNode(8);
    auto* node2 = new TreeNode(6);
    auto* node3 = new TreeNode(6);
    auto* node4 = new TreeNode(5);
    auto* node5 = new TreeNode(7);
    auto* node6 = new TreeNode(7);
    auto* node7 = new TreeNode(5);
    ConnectTreeNode(node1, node2, node3);
    ConnectTreeNode(node2, node4, node5);
    ConnectTreeNode(node3, node6, node7);
    return node1;
}

bool isSymmetrical(TreeNode* pLeft, TreeNode* pRight) {
    if (pLeft == nullptr && pRight == nullptr) {
        return true;
    }
    if (pLeft == nullptr || pRight == nullptr) {
        return false;
    }
    if (pLeft->val != pRight->val) {
        return false;
    }
    return isSymmetrical(pLeft->left, pRight->right) && isSymmetrical(pLeft->right, pRight->left);
}

bool isSymmetrical(TreeNode* pRoot) {
    if (pRoot == nullptr) {
        return true;
    }
    return isSymmetrical(pRoot->left, pRoot->right);
}

int main() {
    TreeNode* pRoot = BuildTree1();
    bool res = isSymmetrical(pRoot);
    cout << res << endl;
    return 0;
}